home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / text / hyper / hsc_source.lha / hsc / source / hsclib / include.c < prev    next >
C/C++ Source or Header  |  1996-11-17  |  9KB  |  312 lines

  1. /*
  2.  * hsclib/include.c
  3.  *
  4.  * hsc include functions
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 17-Nov-1996
  23.  * created: 19-Feb-1996
  24.  */
  25.  
  26. #include <time.h>
  27.  
  28. #include "hsclib/inc_base.h"
  29.  
  30. #include "ugly/ufile.h"
  31. #include "ugly/fname.h"
  32.  
  33. #include "hsclib/input.h"
  34. #include "hsclib/parse.h"
  35.  
  36. #define NOEXTERN_HSCLIB_INCLUDE_H
  37. #include "hsclib/include.h"
  38.  
  39. /*
  40.  * hsc_include
  41.  *
  42.  * read from inpf, parse for hsc-commands and execute them,
  43.  * check for html-error,
  44.  * write all out to outf and close input file.
  45.  *
  46.  * params: inpfnm...input file name
  47.  *         outf.....output file structure, already opended
  48.  *
  49.  * result: TRUE, if all worked well, else FALSE
  50.  */
  51. static BOOL hsc_include(HSCPRC * hp, INFILE * inpf, ULONG optn, INFILEPOS * base_pos)
  52. {
  53.     BOOL ok;                    /* result */
  54.     ok = (inpf != NULL);
  55.  
  56.     if (optn & IH_POS_PARENT)
  57.     {
  58.         panic("IH_POS_PARENT set");
  59.     }
  60.  
  61.     if (inpf)                   /* file opened? */
  62.     {                  
  63. #if (defined MSDOS && !defined HSC_YOUR)
  64.         /* check input > 32K */
  65.         if (estrlen(inpf->lnbuf) > (32*1024))
  66.         {
  67.             fprintf(stderr, "** input file too huge\n");
  68.             exit(RC_FAIL);
  69.         }
  70. #endif
  71.         /* push current input file on input-file-stack */
  72.         if (hp->inpf)
  73.             ins_dlnode(hp->inpf_stack, hp->inpf_stack->first, (APTR) hp->inpf);
  74.  
  75.         /* set new base position for input-file */
  76.         /* (if called from a macro or after eg. <$source>) */
  77.         if (base_pos)
  78.             set_infile_base(inpf, base_pos);
  79.  
  80.         /* assign new input file to hsc-process */
  81.         hp->inpf = inpf;
  82.  
  83.         /* hide status? */
  84.         if ((optn & IH_PARSE_MACRO) || (optn & IH_PARSE_MACRO))
  85.             optn |= IH_NO_STATUS;
  86.  
  87.         /* set char-parse methods */
  88.         inpf->is_nc = hsc_normch;       /* set is_nc-methode */
  89.         inpf->is_ws = hsc_whtspc;       /* set is_ws-methode */
  90.  
  91.         /* status message: reading new file */
  92.         if (!(optn & IH_NO_STATUS) && !infeof(inpf))
  93.             hsc_status_file_begin(hp, infget_fname(hp->inpf));
  94.  
  95.         while (!infeof(inpf) && ok)
  96.         {                       /* parse file */
  97.             if (!(optn & IH_NO_STATUS) &&       /* status message */
  98.                 (hp->prev_status_line != infget_y(hp->inpf))
  99.                 )
  100.             {
  101.                 hsc_status_line(hp);
  102.                 hp->prev_status_line = infget_y(hp->inpf);
  103.             }
  104.             /* parse next item */
  105.             if (optn & IH_PARSE_SOURCE)
  106.                 ok = hsc_parse_source(hp);
  107.             else
  108.                 ok = hsc_parse(hp);
  109.         }
  110.  
  111.         /* parse at end: check for missing tags, .. */
  112.         if (ok && (optn & IH_PARSE_END))
  113.         {                       /* parse end (unclosed tags etc) */
  114.             ok = hsc_parse_end(hp);
  115.  
  116.             if (ok && (optn & IH_UPDATE_PRJ))
  117.                 ok = hsc_parse_end_id(hp);      /* update project file */
  118.         }
  119.  
  120.         /* end of file status */
  121.         if (!(optn & IH_NO_STATUS))
  122.         {
  123.             hsc_status_file_end(hp);    /* status message: file processed */
  124.         }
  125.         infclose(hp->inpf);     /*    close file */
  126.  
  127.         /* pull previous input file from input-file-stack
  128.          * or end hsc-process
  129.          */
  130.         if (hp->inpf_stack->first)
  131.         {
  132.             /* pull first item from stack */
  133.             hp->inpf = (INFILE *) hp->inpf_stack->first->data;
  134.             hp->inpf_stack->first->data = NULL;
  135.  
  136.             del_dlnode(hp->inpf_stack, hp->inpf_stack->first);
  137.         }
  138.         else
  139.         {
  140.             hp->inpf = NULL;
  141.         }
  142.  
  143.     }
  144.     else
  145.         panic("no input file");
  146.  
  147.     return (ok);
  148. }
  149.  
  150. /*
  151.  * hsc_include_file
  152.  *
  153.  * open input file and include it
  154.  */
  155. BOOL hsc_base_include_file(HSCPRC * hp, STRPTR filename, ULONG optn, INFILEPOS * base_pos)
  156. {
  157.     BOOL ok = FALSE;
  158.     INFILE *inpf = NULL;
  159.  
  160.     /* status message: reading input */
  161.     if (!(optn & (IH_PARSE_MACRO | IH_PARSE_HSC)))
  162.     {
  163.         hsc_status_file_begin(hp, filename);
  164.     }
  165.  
  166.     /* check for stdin to use as input-file */
  167.     if (!strcmp(filename, FILENAME_STDIN))
  168.         filename = NULL;
  169.  
  170.     /* open & read input file */
  171.     errno = 0;
  172.     inpf = infopen(filename, ES_STEP_INFILE);
  173.  
  174.     if (inpf)
  175.     {
  176.         /* include opened file */
  177.         ok = hsc_include(hp, inpf, optn, base_pos);
  178.  
  179.         /* check if this file is the main-source-file
  180.          * or an include-file and update project-data
  181.          * if neccessary
  182.          */
  183.         if (ok && hp->project)
  184.         {
  185.             if (optn & IH_IS_SOURCE)
  186.             {
  187.                 if (!filename)
  188.                     filename = FILENAME_STDIN;
  189.                 D(fprintf(stderr, DHL "INCLUDE source: `%s'\n", filename));
  190.                 hsc_project_set_source(hp->project, filename);
  191.                 /*reallocstr(&(hp->document->sourcename), filename); */
  192.             }
  193.  
  194.             /* check if this file is an include-file
  195.              * and update project-data if neccessary */
  196.             if (filename && (optn & IH_IS_INCLUDE))
  197.             {
  198.                 D(fprintf(stderr, DHL "INCLUDE subfile: `%s'\n", filename));
  199.                 hsc_project_add_include(hp->project, filename);
  200.             }
  201.         }
  202.     }
  203.     else
  204.     {
  205.         hsc_msg_noinput(hp, filename);  /* couldn't open file */
  206.     }
  207.  
  208.     return (ok);
  209. }
  210.  
  211. /*
  212.  * hsc_include_string
  213.  *
  214.  * open string as input file and include it
  215.  */
  216. BOOL hsc_base_include_string(HSCPRC * hp, STRPTR filename, STRPTR s, ULONG optn, INFILEPOS * base_pos)
  217. {
  218.     BOOL ok;
  219.     INFILE *inpf = NULL;
  220.  
  221.     if (optn & IH_POS_PARENT)
  222.     {
  223.         filename = PARENT_FILE_ID;
  224.         optn &= ~IH_POS_PARENT;
  225.     }
  226.     inpf = infopen_str(filename, s, 0);         /* try to open input file */
  227.  
  228.     ok = hsc_include(hp, inpf, optn, base_pos);
  229.  
  230.     return (ok);
  231. }
  232.  
  233. /*
  234.  * find_includefile
  235.  *
  236.  * scan all include-directories for file to be opened;
  237.  * if the file can't be found, the filename in the current
  238.  * directory will be returned (which should therefor result
  239.  * in an error while processing hsc_include())
  240.  *
  241.  * NOTE: this function considers the file to be found if it could have
  242.  *       been opened for input. afterwards, the file is immediatly
  243.  *       closed. This isn't really a multitasking-conform behavior
  244.  *       because the file will have to be reopend again by the
  245.  *       hsc_include() function later and meanwhile could have been
  246.  *       removed by another task.
  247.  */
  248. static BOOL find_includefile(HSCPRC *hp, EXPSTR * dest, STRPTR filename)
  249. {
  250.     BOOL found = FALSE;
  251.  
  252.     /* reset filename */
  253.     set_estr(dest, filename);
  254.  
  255.     if (!fexists(filename))
  256.     {
  257.         DLNODE *nd = dll_first(hp->include_dirs);
  258.  
  259.         /* process all include-directories.. */
  260.         while (nd && !found)
  261.         {
  262.             /* concat incdir+filename, check if it exists,
  263.              * process next or abort loop */
  264.             link_fname(dest, (STRPTR) dln_data(nd), filename);
  265.             D(fprintf(stderr, DHL "  try `%s'\n", estr2str(dest)));
  266.             if (fexists(estr2str(dest)))
  267.             {
  268.                 found = TRUE;
  269.             }
  270.             else
  271.                 nd = dln_next(nd);
  272.         }
  273.     }
  274.     else
  275.     {
  276.         /* found in current directory */
  277.         found = TRUE;
  278.     }
  279.  
  280.     if (found)
  281.     {
  282.         D(fprintf(stderr, DHL "  found at `%s'\n", estr2str(dest)));
  283.     }
  284.  
  285.     return (found);
  286. }
  287.  
  288. /* hsc_include_file: include file without base-position */
  289. BOOL hsc_include_file(HSCPRC * hp, STRPTR filename, ULONG optn)
  290. {
  291.     BOOL ok = FALSE;
  292.     EXPSTR *real_filename = init_estr(64);
  293.  
  294.     /* scan include directories */
  295.     find_includefile(hp, real_filename, filename);
  296.  
  297.     /* now include file */
  298.     ok = hsc_base_include_file(hp, estr2str(real_filename), optn, NULL);
  299.  
  300.     /* cleanup */
  301.     del_estr(real_filename);
  302.  
  303.     return ok;
  304. }
  305.  
  306. /* hsc_include_string: include string without base-position */
  307. BOOL hsc_include_string(HSCPRC * hp, STRPTR filename, STRPTR s, ULONG optn)
  308. {
  309.     return (hsc_base_include_string(hp, filename, s, optn, NULL));
  310. }
  311.  
  312.